package com.demo.studentmanager;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.demo.studentmanager.databinding.ActivityEditStudentInfoBinding;

public class EditStudentInfoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityEditStudentInfoBinding binding = ActivityEditStudentInfoBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        boolean self = getIntent().getBooleanExtra("self", false);
        String phone = getIntent().getStringExtra("phone");
        DBHelper dbHelper = new DBHelper(this);
        User user = dbHelper.getUserByPhone(phone);
        binding.etPhone.setText(user.getAccount());
        binding.etAccount.setText(user.getName());
        binding.etClass.setText(user.getClazz());
        binding.etPassword.setText(user.getPassword());
        binding.etPasswordSure.setText(user.getPassword());
        binding.etSid.setText(user.getSid());
        binding.btnDelete.setVisibility(self ? View.GONE : View.VISIBLE);
        binding.btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = binding.etAccount.getText().toString();
                String clazz = binding.etClass.getText().toString();
                String phone = binding.etPhone.getText().toString();
                String password = binding.etPassword.getText().toString();
                String passwordSure = binding.etPasswordSure.getText().toString();
                String sex = binding.etSid.getText().toString();
                if (name.isEmpty() || clazz.isEmpty() || phone.isEmpty()|| sex.isEmpty()) {
                    Toast.makeText(EditStudentInfoActivity.this, "请完成所有内容！", Toast.LENGTH_SHORT).show();
                    return;
                }
                User user = new User();
                user.setName(name);
                user.setClazz(clazz);
                user.setAccount(phone);
                if (!password.isEmpty() || !passwordSure.isEmpty()) {
                    if (!password.equals(passwordSure)) {
                        Toast.makeText(EditStudentInfoActivity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    user.setPassword(password);
                }
                user.setSid(sex);
                dbHelper.updateUser(user);
                Toast.makeText(EditStudentInfoActivity.this, "保存成功！", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
        binding.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(EditStudentInfoActivity.this)
                        .setTitle("注意")
                        .setMessage("确定要删除此学生吗？")
                        .setPositiveButton("取消", null)
                        .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dbHelper.deleteUser(user);
                                Toast.makeText(EditStudentInfoActivity.this, "删除成功！", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }).show();
            }
        });
    }

    public void back(View view) {
        finish();
    }
}